package cn.lanqiao.book.controller;

import cn.lanqiao.book.pojo.ShelfBook;
import cn.lanqiao.book.service.impl.BookShelfServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/BookShelf")
public class BookShelfServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String method=req.getParameter("_method");
        try {
            Method m= getClass().getMethod(method,HttpServletRequest.class,HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    public void addBookToShelf(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //获取书籍的id
        int book_id= Integer.parseInt(req.getParameter("b_id"));
        HttpSession session=req.getSession();
        //获取当前用户名
        String u_username= (String) session.getAttribute("username");
        //如果用户未登录
        if(u_username==null){
            resp.getWriter().write("请先登录");
            return;
        }
        if(BookShelfServiceImpl.getInstance().addBookToShelf(book_id,u_username)){
            resp.getWriter().write("加入书架成功");
        }else {
            resp.getWriter().write("加入书架失败");
        }
    }
    public void deleteMyBookShelf(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //获取书籍的id
        int book_id= Integer.parseInt(req.getParameter("b_id"));
        HttpSession session=req.getSession();
        //获取当前用户名
        String u_username= (String) session.getAttribute("username");
        //如果用户未登录
        if(u_username==null){
            resp.getWriter().write("请先登录");
            return;
        }
        if(BookShelfServiceImpl.getInstance().deleteMyBookShelf(book_id,u_username)){
            resp.getWriter().write("移出书架成功");
        }else {
            resp.getWriter().write("移出书架失败");
        }
    }
    public void deleteTempBookShelf(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //获取书籍的id
        int book_id= Integer.parseInt(req.getParameter("b_id"));
        HttpSession session=req.getSession();
        //获取当前用户名
        String u_username= (String) session.getAttribute("username");
        //如果用户未登录
        if(u_username==null){
            resp.getWriter().write("请先登录");
            return;
        }
        if(BookShelfServiceImpl.getInstance().deleteTempBookShelf(book_id,u_username)){
            resp.getWriter().write("移出书架成功");
        }else {
            resp.getWriter().write("移出书架失败");
        }
    }
    //永久书架
    public void myBookShelf(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        String username= (String) session.getAttribute("username");
        //未登录 返回首页
        if(username==null||username==""){
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
        List<ShelfBook> list= BookShelfServiceImpl.getInstance().getBookShelf(username);
        req.setAttribute("bookList",list);
        //将请求转发到 我的书架界面
        req.getRequestDispatcher("/permbookshelf.jsp").forward(req,resp);
    }
    //临时书架
    public void tempBookShelf(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session=req.getSession();
        String username= (String) session.getAttribute("username");
        //未登录 返回首页
        if(username==null||username==""){
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
        List<ShelfBook> list= BookShelfServiceImpl.getInstance().getTempBookShelf(username);
        req.setAttribute("bookList",list);

        //将请求转发到 我的书架界面
        req.getRequestDispatcher("/tempbookshelf.jsp").forward(req,resp);
    }

}
